# !python3      # 使用工具:PyCharm
# 用户登录名:yyds_l
# 项目名称: python爬虫     文件名称:获取元素节点的兄弟节点1
# 系统日期:2022/3/15      系统时间:2:11
# 2022/03/15 02:11

"""ok"""
from bs4 import BeautifulSoup
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""


def print_hi():
    # 异常捕捉
    try:
        soup = BeautifulSoup(html, "html.parser")
        tag = soup.find("p")
        print(soup.find_all("a", limit=1)[0])      # 使用 limit 参数限制返回结果的数量
        """
        获取元素节点的兄弟节点 
        tag.next_sibling
        tag.previous_sibling
        来获取下一个和前一个兄弟节点，其中tag.next_sibling是tag的临近的下一个兄弟节点，tag.previous_sibling是tag的临近的前一个兄弟节点。
        """
        print(soup.p.next_sibling)      # 下一个
        print(tag.previous_sibling)     # 上一个
        for sibling in soup.a.next_siblings:
            print(repr(sibling))
        # 多线程调用
        print_hi_two()
        print("i")
    except Exception as err:
        print(err)


def print_hi_two():
    # 异常捕捉
    try:
        print("i2")
    except Exception as err:
        print(err)


if __name__ == '__main__':
    print_hi()
